######################
## REPLICATION CODE for:##
##Hyun, Christopher, Post, Alison E., and Isha Ray. (2017). "Frontline Worker Compliance with Transparency Reforms: Barriers Posed by Family and Financial Responsibilities." Governance. DOI: 10.1111/gove.12268 ##

######################

#SET WORKING DIRECTORY
rm(list=ls()) #remove previous list
setwd("") #put working directory here

# === FIGURE 2: NOTIFICATION COMPLIANCE ===

#LOAD DATA
notifications <- read.csv("notifications.final3.csv", header=TRUE, stringsAsFactors=FALSE)

#CALCULATE ACTUAL OPENINGS FOR EACH VALVE AREA
install.packages("plyr") 
library(plyr)
PerVA.comp <- ddply(notifications, c("Valve.ID","Service.Station","Supply.frequency", "Employee.Name"), summarise, 
                       Actual.opened=sum(Call.Type=="opened"))

#CALCULATE EXPECTED OPENINGS FOR EACH VALVE AREA
#Number of days (153) divided by frequency, rounded down by "floor"
PerVA.comp$Expected.opened <- floor(153/PerVA.comp$Supply.frequency)

#CALCULATE COMPLIANCE PER VALVEMAN (VM)
PerVM.act <- ddply(PerVA.comp, .(Employee.Name), summarise, PerVM.act=sum(Actual.opened))
PerVM.exp <- ddply(PerVA.comp, .(Employee.Name), summarise, PerVM.exp=sum(Expected.opened))
PerVM.comp <- merge(PerVM.act, PerVM.exp, by="Employee.Name")
PerVM.comp$percentage <- PerVM.comp$PerVM.act/PerVM.comp$PerVM.exp

#APPEND SERVICE STATIONS
Service.stations <- PerVA.comp[, c("Employee.Name", "Service.Station")]
Service.stations <- unique(Service.stations)
PerVM.comp <- merge(PerVM.comp, Service.stations, by="Employee.Name")
PerVM.comp$Service.Station <- as.factor(PerVM.comp$Service.Station)

#CREATE FIGURE
PerVM.comp$Employee.Name <-factor(PerVM.comp$Employee.Name, levels=PerVM.comp[order(PerVM.comp$percentage), "Employee.Name"])

install.packages("extrafont")
library(extrafont)
font_import(pattern="[P/p]alatino")
fonttable()
install.packages("ggplot2")
library(ggplot2)

ggplot(data=PerVM.comp, aes(x=Employee.Name, y=percentage, fill=Service.Station, width=0.7)) + 
  geom_bar(position='dodge', stat='identity') + coord_flip() + 
  labs(y = "Actual / Expected Notifications Ratio", x = "Valveman", fill = "Service Station") + 
  scale_fill_grey(start = 0, end = .9) + theme_classic() +
  theme(text=element_text(size=14, family="Palatino Linotype"))

ggsave("figure2.tiff", width = 10, height = 5, units = "in", dpi = 600)


# === TABLE 1: VALVEMEN COMPLIANCE ===

#LOAD DATA
VAdata.anon <- read.csv("VAdata.anon.csv", header=TRUE, stringsAsFactors=FALSE)

#COMPLIANCE PER VALVE AREA (VA)
VAdata.anon$VAcompliance <- VAdata.anon$Actual.opened/VAdata.anon$Expected.opened

#CODE VALVE AREA CLASS AS FACTOR
VAdata.anon.class <- VAdata.anon
VAdata.anon.class[ , c("gen_class")]<-sapply(VAdata.anon.class[ , c("gen_class")], as.factor)
levels(VAdata.anon.class$gen_class)<-c(levels(VAdata.anon.class$gen_class), "low", "medium", "high")
VAdata.anon.class$gen_class[VAdata.anon.class$gen_class == "0.3"] <- "low"
VAdata.anon.class$gen_class[VAdata.anon.class$gen_class == "0.6"] <- "low"
VAdata.anon.class$gen_class[VAdata.anon.class$gen_class == "1"] <- "low"
VAdata.anon.class$gen_class[VAdata.anon.class$gen_class == "1.3"] <- "medium"
VAdata.anon.class$gen_class[VAdata.anon.class$gen_class == "1.6"] <- "medium"
VAdata.anon.class$gen_class[VAdata.anon.class$gen_class == "2"] <- "high"
VAdata.anon.class$gen_class[VAdata.anon.class$gen_class == "2.3"] <- "high"
VAdata.anon.class$gen_class[VAdata.anon.class$gen_class == "2.6"] <- "high"
VAdata.anon.class$gen_class<-droplevels(VAdata.anon.class$gen_class)
VAdata.anon.class$gen_class<-factor(VAdata.anon.class$gen_class, levels = c("low", "medium", "high", "mixed"))

#CLUSTER FUNCTION (for clustering standard errors by valveman)
cl <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  coeftest(fm, vcovCL) }

#M1: GIRLS
class.childgirls.ols <- lm(VAcompliance~gen_class+children_girls, data=VAdata.anon.class)
summary(class.childgirls.ols)
cl(VAdata.anon.class, class.childgirls.ols, VAdata.anon.class$Employee.name.x)

#M2. CHILDREN TOTAL
class.childtot.ols <- lm(VAcompliance~gen_class+children_total, data=VAdata.anon.class)
summary(class.childtot.ols)
cl(VAdata.anon.class, class.childtot.ols, VAdata.anon.class$Employee.name.x)

#M3: WIFE EMPLOYMENT
class.wifeem.ols <- lm(VAcompliance~gen_class+wife_employment_code, data=VAdata.anon.class)
summary(class.wifeem.ols)
cl(VAdata.anon.class, class.wifeem.ols, VAdata.anon.class$Employee.name.x)

#M4.EMPLOYMENT STATUS
class.employmentstat.ols <- lm(VAcompliance~gen_class+em_status_code, data=VAdata.anon.class)
summary(class.employmentstat.ols)
cl(VAdata.anon.class, class.employmentstat.ols, VAdata.anon.class$Employee.name.x)

#M5: VEHICLE
class.vehicle.ols <- lm(VAcompliance~gen_class+vehicle_code, data=VAdata.anon.class)
summary(class.vehicle.ols)
cl(VAdata.anon.class, class.vehicle.ols, VAdata.anon.class$Employee.name.x)

#M6: AGE
class.age.ols <- lm(VAcompliance~gen_class+age, data=VAdata.anon.class)
summary(class.age.ols)
cl(VAdata.anon.class, class.age.ols, VAdata.anon.class$Employee.name.x)

#M7: VALVES
class.valves.ols <- lm(VAcompliance~gen_class+valves_survey, data=VAdata.anon.class) #linear regression
summary(class.valves.ols)
cl(VAdata.anon.class, class.valves.ols, VAdata.anon.class$Employee.name.x)

#M8: FULL MODEL (GIRLS)
class.allgirls.ols <- lm(VAcompliance~gen_class + children_girls + wife_employment_code + em_status_code + vehicle_code + age + valves_survey,
                         data=VAdata.anon.class)
summary(class.allgirls.ols)
cl(VAdata.anon.class, class.allgirls.ols, VAdata.anon.class$Employee.name.x) 

#M9: FULL MODEL (CHILDREN TOTAL)
class.allchild.ols <- lm(VAcompliance~gen_class+children_total + wife_employment_code + em_status_code + vehicle_code + age + valves_survey,
                         data=VAdata.anon.class)
summary(class.allchild.ols)
cl(VAdata.anon.class, class.allchild.ols, VAdata.anon.class$Employee.name.x)

